<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        button {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 200px;
            height: 200px;
            background-color: #000;
            color: #fff;
            border-radius: 50%;  
        }
    </style>
</head>

<body>
    <button>关灯</button>
    
    <script>
        //获得事件源
        var btn = document.querySelector('button')
        // var body = docum.querySelector('body')
        //注册事件
        //设置旗标变量
        var flag = 0
        btn.onclick = function () {
            if (flag == 0) {
                this.innerHTML = '开灯'
                this.style.backgroundColor = '#fff'
                this.style.color = '#000'
                document.body.style.backgroundColor = '#000'
                flag = 1
            } else {
                this.innerHTML = '关灯'
                this.style.backgroundColor = '#000'
                this.style.color = '#fff'
                document.body.style.backgroundColor = '#fff'
                flag = 0
            }

        }
    </script>
</body>

</html>